package xdu.lz.protocol.dubbo;

import xdu.lz.framework.RequestWrap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import xdu.lz.register.LocalRegister;

import java.lang.reflect.Method;

public class NettyServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        RequestWrap requestWrap = (RequestWrap) msg;
        /**
         * 服务端获取注册中心，也可以做负载均衡
         */
        Class serviceImpl = LocalRegister.get(requestWrap.getInterfaceName());
        /**
         * 获取包装的请求信息,通过反射调用方法
         */
        Method method = serviceImpl.getMethod(requestWrap.getMethodName(), requestWrap.getParamType());
        Object result = method.invoke(serviceImpl.newInstance(), requestWrap.getParams());
        String res = "【Netty:" + result.toString()+"】";
        System.out.println("netty server = " + res);
        ctx.writeAndFlush(res);
    }
}
